//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package events_subscribe

import (
	"fmt"

	"atomgit.com/openlinksaas/proto-gen-go.git/events/events_data_anno"
	"atomgit.com/openlinksaas/proto-gen-go.git/events_api"
)

func processDataAnnoEvent(event *events_api.Event, cfgItemList []*CfgItem) (bool, error) {
	var err error
	var msg string

	{
		addAnnoMemberEvent := &events_data_anno.AddAnnoMemberEvent{}
		if event.EventData.MessageIs(addAnnoMemberEvent) {
			err = event.EventData.UnmarshalTo(addAnnoMemberEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 添加成员 %s 到标注项目 %s。", event.UserDisplayName, event.ProjectName, addAnnoMemberEvent.MemberDisplayName, addAnnoMemberEvent.AnnoProjectName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.DataAnnoEvCfg.AddAnnoMember {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		removeAnnoMemberEvent := &events_data_anno.RemoveAnnoMemberEvent{}
		if event.EventData.MessageIs(removeAnnoMemberEvent) {
			err = event.EventData.UnmarshalTo(removeAnnoMemberEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 从标注项目 %s 删除成员 %s", event.UserDisplayName, event.ProjectName, removeAnnoMemberEvent.AnnoProjectName, removeAnnoMemberEvent.MemberDisplayName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.DataAnnoEvCfg.RemoveAnnoMember {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}
	return false, nil
}
